package zoschina.app.ivan.starweather.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.View;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import zoschina.app.ivan.starweather.R;
import zoschina.app.ivan.starweather.util.HttpCallbackListener;
import zoschina.app.ivan.starweather.util.HttpUtil;
import zoschina.app.ivan.starweather.util.Utility;

/**
 * Created by Ivan on 2015/8/24.
 */
public class WeatherActivity extends Activity {
    private LinearLayout todayWeatherLayout;
    private TextView cityNameText;
    private TextView publishText;
    private TextView todayWeatherDespText;
    private TextView todayHighTempText;
    private TextView todayLowTempText;
    private TextView currentDateText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.weather_layout);
        initUIControls();

        showWeather();
    }


    private void initUIControls(){
        todayWeatherLayout=(LinearLayout)findViewById(R.id.today_weather_layout);
        cityNameText=(TextView)findViewById(R.id.city_name);
        publishText=(TextView)findViewById(R.id.publish_text);
        todayWeatherDespText=(TextView)findViewById(R.id.today_weather_desp);
        todayHighTempText=(TextView)findViewById(R.id.today_weather_high_temp);
        todayLowTempText=(TextView)findViewById(R.id.today_weather_low_temp);
        currentDateText=(TextView)findViewById(R.id.current_date);
    }

    private void queryWeatherInfo(String ip) {
        if(TextUtils.isEmpty(ip)){
            return;
        }
        String httpUrl = "http://apis.baidu.com/showapi_open_bus/weather_showapi/ip2weather?ip="+ip;
        HttpUtil.sendBaiduWeatherHttpRequest(httpUrl, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                HttpUtil.handleWeatherResponse(WeatherActivity.this,response);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showWeatherInfo();
                    }
                });
            }

            @Override
            public void onError(Exception e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        publishText.setText("获取天气信息失败，请稍后重试");
                    }
                });
            }
        });
    }

    private void showWeather() {
        if(!isInfoAvaliable()){
            queryWeatherFromServer();
        }else{
            showWeatherInfo();
        }
    }

    private void showWeatherInfo(){
        SharedPreferences prefs= PreferenceManager.getDefaultSharedPreferences(this);
        cityNameText.setText(prefs.getString("city_name",""));
        todayHighTempText.setText(prefs.getString("today_high_temp",""));
        todayLowTempText.setText(prefs.getString("today_low_temp",""));
        todayWeatherDespText.setText(prefs.getString("today_weather_desp",""));
        currentDateText.setText(prefs.getString("date",""));
        publishText.setText(prefs.getString("time",""));
    }

    private void queryWeatherFromServer(){
        queryIpAddress();
    }

    private void queryIpAddress(){
        String httpUrl = "http://ip.taobao.com/service/getIpInfo.php?ip=myip";
        HttpUtil.sendBaiduWeatherHttpRequest(httpUrl, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                String ip=HttpUtil.handleIpAddressResponse(WeatherActivity.this, response);
                queryWeatherInfo(ip);
            }

            @Override
            public void onError(Exception e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        publishText.setText("获取天气信息失败，请稍后重试");
                    }
                });
            }
        });
    }

    private boolean isInfoAvaliable(){
        SharedPreferences prefs= PreferenceManager.getDefaultSharedPreferences(this);
        boolean ret=prefs.getBoolean("avaliable",false);
        return false;
    }
}
